package com.soft.hwx.scdapp.main;

import android.content.Context;
import android.util.Log;

import com.soft.hwx.scdapp.msg.HostSetArgsMsg;
import com.soft.hwx.scdapp.msg.HostWorkCmd;
import com.soft.hwx.scdapp.msg.HostWorkMsg;
import com.soft.hwx.scdapp.msg.RawMsg;
import com.soft.hwx.scdapp.socket.UdpHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;







/*
    侦察主机发送线程类
 */
public class HostSendThread  implements Runnable {
    public    Boolean IsThreadDisable = false;//指示监听线程是否终止

    private LinkedList <RawMsg> msgList = new LinkedList();
    private Object msgListLock = new Object();
    private   Thread updSendThread;
    private UdpHelper udpClient;

    private String dstIP;
    private int dstPort;

    public HostSendThread(String dstIP,int dstPort){
        this.dstIP = dstIP;
        this.dstPort = dstPort;
    }


    /********************************页面1***********************************/
    /*
            设备自检

     */
    public int SelfCheckCmd(){

        return 0;
    }

    /*
            装订参数

     */
    public  int SetTargetArgs(){

        return 0;
    }
    /********************************页面1***********************************/

    /********************************页面2***********************************/
    /*
            启动搜索
     */
    public  int StartSearch(){

        return 0;
    }

    /*
              停止搜索
       */
    public  int StopSearch(){

        return 0;
    }

    public int ClearSearchResult(){
        return 0;
    }

    public int SaveSearchResult(){
        return 0;
    }



    /********************************页面2***********************************/


    /********************************页面3***********************************/
    /*
        交汇定位

     */

      /*
        结果存储

     */


    /********************************页面3***********************************/

    public void AddSendMsg(RawMsg msg){
        //byte [] buf = {(byte)0x44};
        //int len =1;
        //RawMsg message = new RawMsg(buf,len);

        //临界区  保护队列
        synchronized(msgListLock) {
            Log.d("HostSendThread", "加入发送队列");
            msgList.addLast(msg);
        }
    }
    /*
    public int sendWorkMsg(HostWorkMsg msg){
        byte[] buf = msg.getBuf();
        //send buf
        return 0;
    }

    public void reciveUiCmd(Message sendmsg){
        if(sendmsg.what == 21){
            //发送自检命令
            //AddSendMsgMsg((byte[])msg.obj,len);
        }

        synchronized(msgListLock) {
            msgListLock.notify();
        }
    }*/

    public int udpClientSendCmd(RawMsg msg){
        AddSendMsg(msg);

        synchronized(msgListLock) {
            msgListLock.notify();
        }
        return 0;
    }

    public  int StartControl(){
        //创建UDP 客户端
        /*
        Context content =getApplicationContext();
        Map<String,String> mMap= new HashMap<String,String>();
        mMap = readIp(content);
        String ip_addr = mMap.get("ip_addr");
        int port = Integer.parseInt(mMap.get("port"));
        udpClient = new UdpHelper(ip_addr,port);
        Log.d("HostSendThread", "ip_addr:"+ip_addr);
        Log.d("HostSendThread", "port:"+Integer.toString(port));*/
        udpClient = new UdpHelper(dstIP,dstPort);
        //udpClient = new UdpHelper("192.168.0.171",8013);
        Log.d("HostSendThread", "ip_addr:"+dstIP);
        Log.d("HostSendThread", "port:"+Integer.toString(dstPort));
        updSendThread = new Thread(udpClient);
        try {
            updSendThread.start();
            Log.d("HostSendThread", "创建UDP 发送线程成功");

        }catch (Exception e){
            Log.d("HostSendThread", "创建UDP 发送线程失败:"+e.getMessage());
            return -1;
        }
        return 0;
    }

    public void WorkOrderIssued(RawMsg msg){
        HostWorkMsg workMsg = (HostWorkMsg)msg;
        workMsg.ToBuf();
        udpClient.send2(workMsg.getBuf(),workMsg.length+4);
    }

    @Override
    public void run(){
        StartControl();
        while(!IsThreadDisable){
            if(msgList.size()<=0){
                try {
                    //主线程等待通过notify() 唤醒
                    Log.d("HostSendThread", "发送队列被唤醒");
                    synchronized(msgListLock) {
                        msgListLock.wait();
                    }
                }catch (Exception e){

                }
            }
            RawMsg msg = null;
            //临界区  保护队列
            synchronized(msgListLock) {
                msg = msgList.getFirst();
                msgList.removeFirst();
            }

            if(msg==null)
                continue;

                //System.out.println(Arrays.toString(msg.getBuf()));
                Log.d("HostSendThread", "唤醒发送队列");
                //调用子类的tobuf 生成发送报文 buf
                switch (msg.workCmd){
                    case HostWorkCmd.selfCheck:
                        Log.d("HostSendThread", "自检命令");
                        WorkOrderIssued(msg);
                        break;
                    case HostWorkCmd.setArgs:
                        Log.d("HostSendThread", "参数装订");
                        HostSetArgsMsg setArgsMsg = (HostSetArgsMsg)msg;
                        Log.d("HostSendThread", "目标1频率上限:"+String.format(" %d",setArgsMsg.t1UpperLimitCarrierFreq));
                        setArgsMsg.ToBuf();
                        udpClient.send2(setArgsMsg.getBuf(),setArgsMsg.length+4);
                        break;
                    case HostWorkCmd.search:
                        WorkOrderIssued(msg);
                        break;
                    case HostWorkCmd.releaseSearch:
                        WorkOrderIssued(msg);
                    break;
            }
        }
    }
}

